﻿using Common.Dtos;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using Sigma.Admin.Client.DialogAware;
using Sigma.Admin.Client.Extensions;
using Sigma.Admin.Client.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sigma.Admin.Client.ViewModels.DialogVM
{
    public class AddMenuViewModel: BindableBase, IDialogHostAware
    {
        private readonly IMenuService _menuService;
        private readonly IEventAggregator _ea;
        public string DialogHostName { get; set; }
        public DelegateCommand SaveCommand { get; set; }
        public DelegateCommand CancelCommand { get; set; }

        public AddMenuViewModel(IMenuService service, IEventAggregator ea)
        {
            _menuService = service;
            SaveCommand = new DelegateCommand(Save);
            CancelCommand = new DelegateCommand(Cancel);
            _ea = ea;
        }
        private MenuDto menuDto=new MenuDto();

        public MenuDto MenuDto
        {
            get { return menuDto; }
            set { menuDto = value; RaisePropertyChanged(); }
        }
        private ObservableCollection<MenuDto> _parentMenus;

        public ObservableCollection<MenuDto> ParentMenus
        {
            get { return _parentMenus; }
            set { _parentMenus = value; RaisePropertyChanged(); }
        }

        private MenuDto _selectedParent;

        public MenuDto SelectedParent
        {
            get { return _selectedParent; }
            set { _selectedParent = value; RaisePropertyChanged(); }
        }

        private async void Save()
        {
            if (string.IsNullOrWhiteSpace(menuDto.TitleName))
            {
                _ea.SendMessage("菜单名不能为空","AddMenu");
                return;
            }
            if(_selectedParent!=null)
                menuDto.PId = _selectedParent.Id;
            if (DialogHost.IsDialogOpen(DialogHostName))
            {
                if (menuDto.Level == 0)
                {
                    menuDto.PId = 0;
                    menuDto.IsView = false;
                }
                else if (menuDto.Level == 1)
                {
                    
                    if(string.IsNullOrWhiteSpace(menuDto.Target)||menuDto.PId==0)
                    {
                        _ea.SendMessage("目标视图和父级菜单不能为空", "AddMenu");
                        return;
                    }
                    menuDto.IsView = true;
                }
                var res = await _menuService.AddMenu(menuDto);
                if (res.Status)
                {
                    _ea.SendMessage("", "UpdateMenu");
                    DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK));
                    _ea.SendMessage(res.Message ?? "响应成功");
                }
                else
                {
                    _ea.SendMessage(res.Message ?? "服务器无响应", "AddMenu");
                }
            }
                
        }

        private void Cancel()
        {
            if (DialogHost.IsDialogOpen(DialogHostName))
                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
        }
        public void OnDialogOpend(IDialogParameters parameters)
        {
            if (parameters.ContainsKey("value"))
            {
                var menus = parameters.GetValue<List<MenuDto>>("value");
                ParentMenus = new ObservableCollection<MenuDto>(menus);

            }
            else
                ParentMenus = new ObservableCollection<MenuDto>();
        }
    }
}
